package top.xmj.service.impl;

import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.xmj.controller.WebSocketServer;
import top.xmj.dao.UserMapper;
import top.xmj.domain.User;
import top.xmj.service.UserService;
import top.xmj.util.JWTUtils;
import top.xmj.vo.ResultVO;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 冰咖啡
 * @Date:2/12/2022 下午 11:01
 * @Description: 用户服务
 */
@Service
public class UserServiceImpl implements UserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private UserMapper usermapper;

    @Override
    public List<User> getUsers() {
//        List<User> users = usermapper.selectList(null);
        List<User> users = usermapper.getUsers();
        System.out.println(users);
        return users;
    }

    @Override
    public User queryByName(String name) {
        return usermapper.queryByName(name);
    }

    @Override
    public Map<String,Object> login(User u) {
        Map<String,Object> map = new HashMap<>();
        //phone是除id外的唯一标志 需要进行检查
        User user = usermapper.queryByName(u.getUsername());
        log.info("user:"+user);
        map.put("user",user);
        if (user == null) {
           return map;
        }
        //将userId存入token中
        String token = JWTUtils.createToken(user.getUsername().toString());
        map.put("token",token);
        return map;
    }
}
